<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="编号" prop="orgno">
        <el-input
          v-model="queryParams.orgno"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="orgname">
        <el-input
          v-model="queryParams.orgname"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:organize:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
          >展开/折叠</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!--  -->
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="organizeList"
      :default-expand-all="isExpandAll"
      border
      row-key="organizeId"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        label="名称"
        align="center"
        fixed
        prop="orgname"
        width="220"
      />
      <el-table-column label="编号" align="center" prop="orgno" width="120" />
      <el-table-column
        label="组织类型"
        align="center"
        width="100"
        prop="orgFlg"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_org_flag"
            :value="scope.row.orgFlg"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否有效" align="center" prop="activeFlag">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_active_flag"
            :value="scope.row.activeFlag"
          />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="组织类型" align="center" prop="orgType" /> -->
      <el-table-column label="结算单位" align="center" prop="jsdwName" />
      <el-table-column label="发货单位" align="center" prop="fhdwName" />
      <el-table-column label="电话" align="center" prop="telephoneno" />
      <el-table-column label="传真" align="center" prop="fax" />
      <el-table-column label="网址" align="center" prop="webaddress" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="地址" align="center" prop="address" width="330" />
      <el-table-column label="邮政编码" align="center" prop="postcode" />
      <el-table-column
        label="营业执照"
        align="center"
        prop="liscenseno"
        width="200"
      />
      <el-table-column
        label="发票抬头"
        align="center"
        prop="invoiceheader"
        width="200"
      />
      <el-table-column label="省" align="center" prop="varchar3" />
      <el-table-column label="市" align="center" prop="varchar4" />
      <el-table-column label="县" align="center" prop="varchar5" />
      <el-table-column label="中文简称" align="center" prop="varchar7" />
      <el-table-column label="英文简称" align="center" prop="jc" />
      <el-table-column
        label="单位性质"
        align="center"
        prop="varchar6"
        width="100"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_org_nature"
            :value="scope.row.varchar6"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="上级" align="center" prop="parentId" /> -->
      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:organize:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:organize:add']"
            >新增</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:organize:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改组织结构对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="编号" prop="orgno">
              <el-input v-model="form.orgno" placeholder="请输入编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="orgname">
              <el-input v-model="form.orgname" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="组织标记">
              <el-radio-group v-model="form.orgFlg">
                <el-radio
                  v-for="dict in dict.type.sys_org_flag"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否有效">
              <el-radio-group v-model="form.activeFlag">
                <el-radio
                  v-for="dict in dict.type.sys_active_flag"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="组织列表" prop="parentId">
              <treeselect
                v-model="form.parentId"
                :options="organizeOptions"
                :normalizer="normalizer"
                placeholder="请选择单位"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="结算单位" prop="jsdwId">
              <el-select
                v-model="form.jsdwId"
                placeholder="请选择结算单位"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="dict in organizeUnitList"
                  :key="dict.organizeId"
                  :label="dict.orgname"
                  :value="dict.organizeId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发货单位" prop="fhdwId">
              <el-select
                v-model="form.fhdwId"
                placeholder="请选择发货单位"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="dict in organizeUnitList"
                  :key="dict.organizeId"
                  :label="dict.orgname"
                  :value="dict.organizeId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电话" prop="telephoneno">
              <el-input v-model="form.telephoneno" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="传真" prop="fax">
              <el-input v-model="form.fax" placeholder="请输入传真" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="网址" prop="webaddress">
              <el-input v-model="form.webaddress" placeholder="请输入网址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="form.contact" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮政编码" prop="postcode">
              <el-input v-model="form.postcode" placeholder="请输入邮政编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="营业执照" prop="liscenseno">
              <el-input
                v-model="form.liscenseno"
                placeholder="请输入营业执照"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票抬头" prop="invoiceheader">
              <el-input
                v-model="form.invoiceheader"
                placeholder="请输入发票抬头"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="中文简称" prop="varchar7">
              <el-input v-model="form.varchar7" placeholder="请输入中文简称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文简称" prop="jc">
              <el-input v-model="form.jc" placeholder="请输入英文简称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单位类型" prop="varchar6">
              <el-select v-model="form.varchar6" placeholder="请选择单位类型">
                <el-option
                  v-for="dict in dict.type.sys_org_nature"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="省" prop="varchar3">
              <el-input v-model="form.varchar3" placeholder="请输入省" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="市" prop="varchar4">
              <el-input v-model="form.varchar4" placeholder="请输入市" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="县" prop="varchar5">
              <el-input v-model="form.varchar5" placeholder="县" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="描述" prop="descText">
              <el-input
                v-model="form.descText"
                type="textarea"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrganize,
  getOrganize,
  delOrganize,
  addOrganize,
  updateOrganize,
} from "@/api/system/organize";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Organize",
  dicts: ["sys_org_nature", "sys_active_flag", "sys_org_flag"],
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 组织结构表格数据
      organizeList: [],
      // 组织结构单位数据 organizeUnitList
      organizeUnitList: [],
      // 组织结构树选项
      organizeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        orgno: null,
        orgname: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orgno: [{ required: true, message: "编号不能为空", trigger: "blur" }],
        orgname: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        parentId: [
          { required: true, message: "上级单位不能为空", trigger: "blur" },
        ],
        // jsdwId: [
        //   { required: true, message: "结算单位不能为空", trigger: "blur" },
        // ],
        // fhdwId: [
        //   { required: true, message: "发货单位不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询组织结构列表 */
    getList() {
      this.loading = true;
      listOrganize(this.queryParams).then((response) => {
        this.organizeList = this.handleTree(
          response.data,
          "organizeId",
          "parentId"
        );
        this.organizeUnitList = response.data.filter(
          (item) => item.orgFlg == 1
        );
        this.loading = false;
      });
    },
    /** 转换组织结构数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.organizeId,
        label: node.orgname,
        children: node.children,
      };
    },
    /** 查询组织结构下拉树结构 */
    getTreeselect() {
      listOrganize().then((response) => {
        this.organizeOptions = [];
        const data = {
          organizeId: 0,
          orgname: "集团所有单位",
          children: [],
        };
        data.children = this.handleTree(
          response.data,
          "organizeId",
          "parentId"
        );
        this.organizeOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        organizeId: null,
        orgno: null,
        orgname: null,
        orgFlg: "1",
        activeFlag: "1",
        orgType: null,
        descText: null,
        telephoneno: null,
        fax: null,
        email: null,
        webaddress: null,
        address: null,
        postcode: null,
        contact: null,
        vorgtype: null,
        areacode: null,
        tradetype: null,
        orgstyle: null,
        orgkind: null,
        juridicalperson: null,
        liscenseno: null,
        enrolladdr: null,
        bankname: null,
        bankaccountno: null,
        taxno: null,
        taxrate: null,
        invoiceheader: null,
        moneytype: null,
        foreignname: null,
        subjectno: null,
        creditclass: null,
        creditmax: null,
        date1: null,
        date2: null,
        date3: null,
        date4: null,
        boolean1: null,
        boolean2: null,
        boolean3: null,
        boolean4: null,
        datetime1: null,
        datetime2: null,
        datetime3: null,
        datetime4: null,
        time1: null,
        time2: null,
        time3: null,
        time4: null,
        char1: null,
        char2: null,
        char3: null,
        char4: null,
        int1: null,
        int2: null,
        int3: null,
        int4: null,
        long1: null,
        long2: null,
        long3: null,
        long4: null,
        number1: null,
        number2: null,
        number3: null,
        number4: null,
        varchar1: null,
        varchar2: null,
        varchar3: null,
        varchar4: null,
        varchar5: null,
        varchar6: null,
        varchar7: null,
        varchar8: null,
        varchar9: null,
        varchar10: null,
        text1: null,
        html1: null,
        image1: null,
        sysCreated: null,
        sysCreatedby: null,
        sysPostn: null,
        sysDivision: null,
        sysOrg: null,
        sysLastUpd: null,
        sysLastUpdBy: null,
        classcode: null,
        ddid: null,
        qyweixinorgid: null,
        ddunionid: null,
        dingid: null,
        parentId: null,
        rootorgId: null,
        basepostnFk: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.organizeId) {
        this.form.parentId = row.organizeId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加组织结构";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getOrganize(row.organizeId).then((response) => {
        this.form = response.data;
        this.form.orgFlg = this.form.orgFlg.toString();
        this.form.activeFlag = this.form.activeFlag.toString();
        this.open = true;
        this.title = "修改组织结构";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.organizeId != null) {
            updateOrganize(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrganize(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除组织名称为"' + row.orgname + '"的数据项？')
        .then(function () {
          return delOrganize(row.organizeId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
